package 我的Java学习_算法基础.day_02;

public class _028_在有空字符串中的有序字符串数组中查找 {
    public static void main(String[] args) {
        String[] a = {"a","","ac","","ad","b","","ba"};
        int res=indexOf(a,"abc");
        System.out.println(res);
        res=indexOf(a,"ac");
        System.out.println(res);
    }
    private static int indexOf(String[] arr,String target){
        int begin = 0;
        int end = arr.length-1;
        while(begin<=end){
            int indexOfMid = begin +((end-begin)>>1);
            while(arr[indexOfMid].equals("")){
                indexOfMid++;
                if(indexOfMid>end){//abc时，无此代码块，死循环
                    return -1;
                }
            }
            if(target.compareTo(arr[indexOfMid])<0){
                end=indexOfMid-1;
            }else if(target.compareTo(arr[indexOfMid])>0){
                begin=indexOfMid+1;
            }else{
                return indexOfMid;
            }
        }
        return -1;
    }
}
